<!-- 知识小册 -->
<template>
  <el-container>
    <el-main>
      <!-- 搜索 -->
      <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="名称">
            <el-input
              v-model="formInline.title"
              placeholder="请输入知识库名称"
            />
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="formInline.status" placeholder="请选择状态">
              <el-option label="已发布" value="1" />
              <el-option label="草稿" value="0" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getList">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <el-card>
        <div
          style="
            width: 100%;
            display: flex;
            justify-content: end;
            margin-bottom: 12px;
          "
        >
          <el-button type="primary" @click="add"> + 新建</el-button>

          <!-- 抽屉 -->
          <el-drawer
            v-model="drawer"
            :title="flag + '知识库'"
            direction="rtl"
            :before-close="handleClose"
          >
            <el-form :model="form">
              <el-form-item label="名称">
                <el-input v-model="form.title" />
              </el-form-item>

              <el-form-item label="描述">
                <el-input v-model="form.description" />
              </el-form-item>

              <el-form-item label="评论">
                <el-switch v-model="form.comment" />
              </el-form-item>

              <el-form-item label="封面">
                <el-upload
                  class="upload-demo"
                  drag
                  action="/api/upload"
                  multiple
                  :on-change="changeFile"
                >
                  <div v-if="!imageUrl">
                    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                    <div class="el-upload__text">
                      点击选择文件或将文件拖拽到此处
                    </div>
                  </div>
                  <el-image :src="imageUrl" v-else class="avatar"></el-image>
                </el-upload>
              </el-form-item>
            </el-form>

            <template #footer>
              <div style="flex: auto">
                <el-button @click="handleClose">取消</el-button>

                <el-button
                  type="primary"
                  @click="confirmClick"
                  :disabled="disabled"
                >
                  {{ flag }}
                </el-button>
              </div>
            </template>
          </el-drawer>
        </div>

        <!-- 表格 -->
        <div class="list">
          <div v-for="(item, index) in list" :key="index" class="list-item">
            <el-image :src="item.url"> </el-image>
            <div class="item-info">
              <p>{{ item.title }}</p>
              <p>{{ item.description }}</p>
            </div>
            <div class="item-action">
              <!-- 编辑 -->
              <edit-outlined />

              <!-- 发布 -->
              <el-tooltip
                v-if="item.status"
                class="box-item"
                effect="dark"
                content="发布线上"
                placement="top"
              >
                <cloud-upload-outlined @click="issue(item.id)" />
              </el-tooltip>

              <!-- 下线 -->
              <el-tooltip
                v-else
                class="box-item"
                effect="dark"
                content="设为草稿"
                placement="top"
              >
                <cloud-download-outlined @click="issue(item.id)" />
              </el-tooltip>

              <!-- 设置 -->
              <setting-outlined @click="edit(item)" />

              <!-- 删除 -->
              <el-popconfirm
                title="确认删除?"
                confirm-button-text="确认"
                cancel-button-text="取消"
                @confirm="delItem(item.id)"
              >
                <template #reference>
                  <delete-outlined />
                </template>
              </el-popconfirm>
            </div>
          </div>
        </div>

        <!-- 分页器 -->
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="currentChange"
        >
        </el-pagination>
      </el-card>
    </el-main>
  </el-container>
</template>

<script setup>
import {
  apiKnowledge,
  apiIssueKnowledge,
  apiKnowledgeDel,
  apiAddKnowledge,
  apiEditKnowledge,
} from "@/api/knowledge";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  EditOutlined,
  CloudUploadOutlined,
  CloudDownloadOutlined,
  DeleteOutlined,
  SettingOutlined,
} from "@ant-design/icons-vue";
import { UploadFilled } from "@element-plus/icons-vue";

/**************************************************/

const disabled = ref(true);

const list = ref([]);

const page = ref({
  current: 1,
  size: 10,
});

const total = ref(0);

const formInline = ref({});

const drawer = ref(false);

const flag = ref("");

const form = ref({});

const imageUrl = ref("");

/**************************************************/

// 重置
const reset = () => {
  formInline.value = {};

  getList();
};

// 切换分页
const currentChange = (pageIndex) => {
  page.value.current = pageIndex;

  getList();
};

// 发布/草稿
const issue = (id) => {
  apiIssueKnowledge({ id }).then((res) => {
    ElMessage({
      message: res.msg,
      type: "success",
    });

    getList();
  });
};

// 删除
const delItem = (id) => {
  apiKnowledgeDel({ id }).then((res) => {
    ElMessage({
      message: res.msg,
      type: "success",
    });
    getList();
  });
};

// 新增
const add = () => {
  form.value = {};

  imageUrl.value = "";

  flag.value = "新增";

  drawer.value = true;
};

// 编辑
const edit = (item) => {
  form.value = item;

  imageUrl.value = item.url;

  flag.value = "更新";

  drawer.value = true;

  disabled.value = false;
};

// 上传
const changeFile = (file) => {
  if (file.response) {
    form.value.url = file.response.url;

    imageUrl.value = file.response.url;

    disabled.value = false;
  }
};

const handleClose = () => {
  drawer.value = false;
};

const confirmClick = () => {
  handleClose();

  if (flag.value === "新增") {
    apiAddKnowledge(form.value).then((res) => {
      ElMessage({
        message: res.msg,
        type: "success",
      });
    });
  } else {
    apiEditKnowledge(form.value).then((res) => {
      ElMessage({
        message: res.msg,
        type: "success",
      });
    });
  }

  getList();
};

/**************************************************/

const getList = () => {
  apiKnowledge({ ...page.value, ...formInline.value }).then((res) => {
    list.value = res.data;

    total.value = res.total;
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.list {
  flex: 1;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}

.list-item {
  flex-shrink: 0;
  margin: 1%;
  width: 18%;
  height: 230px;
  border: 1px solid rgb(230, 230, 230);
  display: flex;
  flex-direction: column;
  .el-image {
    width: 100%;
    flex: 1;
  }
  .item-info {
    padding-left: 10px;
    height: 60px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .item-action {
    border-top: solid 1px rgb(230, 230, 230);
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    cursor: pointer;
    .anticon:hover {
      color: #0188fb;
    }
  }
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
